package com.internal.Internalhibernate.server.dao;

// Generated Feb 24, 2012 1:32:12 PM by Hibernate Tools 3.3.0.GA

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.internal.Internalhibernate.client.MlImage;
import com.internal.Internalhibernate.server.SessionFactoryUtil;

/**
 * Home object for domain model class MlImage.
 * 
 * @see com.internal.Internalhibernate.client.MlImage
 * @author Hibernate Tools
 */
public class MlImageDAO {

	private static final Log log = LogFactory.getLog(MlImageDAO.class);

	private final SessionFactory sessionFactory = SessionFactoryUtil
			.getSessionFactory();

	public MlImage persist(MlImage transientInstance) {
		log.debug("persisting MlImage instance");
		Transaction trans = null;
		MlImage image = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			Query q = session.createQuery("select max(imageId) from MlImage");
			Long lastImgId = (Long) q.uniqueResult();
			if (lastImgId != null)
				transientInstance.setImageId((Long) lastImgId + 1);
			else
				transientInstance.setImageId(new Long(1));
			session.persist(transientInstance);
			session.save(transientInstance);
			trans.commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("persist failed", re);
			throw re;
		}
		return image;
	}

	public void attachDirty(MlImage instance) {
		log.debug("attaching dirty MlImage instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			session.saveOrUpdate(instance);
			trans.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MlImage instance) {
		log.debug("attaching clean MlImage instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			session.lock(instance, LockMode.NONE);
			trans.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(MlImage persistentInstance) {
		log.debug("deleting MlImage instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			session.delete(persistentInstance);
			trans.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("delete failed", re);
			throw re;
		}
	}

	public MlImage merge(MlImage detachedInstance) {
		log.debug("merging MlImage instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			MlImage result = (MlImage) session.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("merge failed", re);
			throw re;
		}
	}

	public MlImage findById(long id) {
		log.debug("getting MlImage instance with id: " + id);
		try {
			Session session = sessionFactory.getCurrentSession();
			session.beginTransaction();
			MlImage instance = (MlImage) session.get("com.internal.Internalhibernate.client.MlImage", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
}
